package com.woniuxy.myfinance.service.ServiceImpl;

import com.woniuxy.commons.entity.Finance;
import com.woniuxy.commons.entity.Inandoutlog;
import com.woniuxy.commons.entity.UserDetails;
import com.woniuxy.commons.entity.Userinfo;

import com.woniuxy.myfinance.mapper.InandoutlogMapper;
import com.woniuxy.myfinance.service.FinanceService;
import com.woniuxy.myfinance.service.InandoutService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author author
 * @create 2021-11-16 16:05
 */
@Service
public class InandoutServiceImpl implements InandoutService {

    @Resource
    private FinanceService financeService;

    @Resource
    private InandoutlogMapper inandoutlogMapper;


    @Override
    public void addInandoutlog(Inandoutlog inandoutlog) {
         //得到用户的区域（调肖帅的接口）
           String province="四川省";  //假设是四川省
        Finance finance=new Finance();
        finance.setProvince(province);//根据省份查找需要改那一条数据
        List<Finance> finances = financeService.selectAllFinance(finance, 1, 5);
        double allmoney = finances.get(0).getAllmoney();
        Integer financeid = finances.get(0).getFinanceid();
        double income = finances.get(0).getIncome();
        double disbursemoney = finances.get(0).getDisbursemoney();
        Finance finance1=new Finance();//修改需传入的对象
           if(inandoutlog.getState().equals("贷款")){
               finance1.setFinanceid(financeid);
               finance1.setProvince(province);
               finance1.setDisbursemoney(disbursemoney+inandoutlog.getMoney());
               finance1.setIncome(income);
//               BigDecimal bigDecimal=new BigDecimal(allmoney);
//               BigDecimal bigDecimal1=new BigDecimal(inandoutlog.getMoney());
//               BigDecimal subtract = bigDecimal.subtract(bigDecimal1);
               finance1.setAllmoney(allmoney-inandoutlog.getMoney());
               financeService.updateFinance(finance1);
           }else {
               finance1.setFinanceid(financeid);
               finance1.setProvince(province);
               finance1.setDisbursemoney(disbursemoney);
               finance1.setIncome(income+inandoutlog.getMoney());
               finance1.setAllmoney(allmoney+inandoutlog.getMoney());
               financeService.updateFinance(finance1);
           }

           inandoutlogMapper.addInandoutlog(inandoutlog);
    }

    @Override
    public void updateInandoutlog(Inandoutlog inandoutlog) {
        inandoutlogMapper.updateInandoutlog(inandoutlog);
    }

    @Override
    public List<Inandoutlog> selectAllInandoutlog(Inandoutlog inandoutlog, Integer pageIndex, Integer pageSize) {
        List<Inandoutlog> inandoutlogs = inandoutlogMapper.selectAllInandoutlog(inandoutlog, pageIndex, pageSize);
        for (Inandoutlog inandoutlog1:inandoutlogs) {
             //根据uid查出用户信息   等待肖帅的接口
            Userinfo userinfo=new Userinfo();
            userinfo.setUserdetails(new UserDetails().setUname("zs").setUaddress("四川省"));
             inandoutlog1.setUserinfo(userinfo);
        }
        return inandoutlogs;
    }

    @Override
    public int selectInandoutlogCountBylimit(Inandoutlog inandoutlog) {
        return inandoutlogMapper.selectInandoutlogCountBylimit(inandoutlog);
    }

    @Override
    public void delectInandoutlogByid(int id) {
        inandoutlogMapper.delectInandoutlogByid(id);
    }
}
